using System;

namespace WGT
{
    public partial class Dialog
    {
        protected bool CheckBox(pos p, Func<bool> get, Action<bool> set, string attrs)
        {
            pos op = null;
            int id = 0;
            string oldC = null, oAttrs = null;
            bool rval = false, oval = false;
            bool value = P(get);

            GetPut(ref op, ref p);
            GetPut(ref oval, ref value);
            GetPut(ref oAttrs, ref attrs);

            if (mode == MODE.SHOW)
            {
                id = iNextId++;

                S(@"<input id='{0}' type='checkbox'{1} onclick=a(w.event(['{0}'])); />", id, value.ToChecked());
                U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                UP(id.ToString(), attrs);
            }

            GetPut(ref id, ref id);
            string sid = id.ToString();

            if (mode == MODE.UPDATE)
            {
                if (oval != value)
                {
                    U(@"$('{0}').checked={1};", sid, value.ToString().ToLower());
                }
                if (!p.Equals(op))
                {
                    U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                }
                if (attrs != oAttrs)
                {
                    UP(sid, attrs);
                }
            }
            else if (mode == MODE.ERASE)
            {
                E(sid);
            }
            else if (mode == MODE.EVENT)
            {
                if (sender == sid)
                {
                    if (set != null) set(!value);
                    bUpdate = true;
                    rval = true;
                }
            }

            if (mode != MODE.ERASE)
            {
                if (bHorizontal) gx += p.w;
                else gy += p.h;
            }

            return rval;
        }
    }
}